#include <stdlib.h>

#include "lib.h"

#include "slotguts.h"
#include "eltoroguts.h"
#include "eltoroslotscommon.h"
#include "slotspin.h"
#include "getrand.h"


bool8 
ElToroSlotGuts::isValidGuts()
{
    return ((getGutsId() >= ElToroCommon::GUTS_MIN) && (getGutsId() <= ElToroCommon::GUTS_MAX));
}


void
ElToroSlotGuts::loadSubGameData()
{
	SlotGuts::loadSubGameData();

//	// If the amount < 500 make it 500
//	if(m_subGame->getData(FrozenCommon::SGIDX_AMOUNT) < FrozenCommon::BASE_AMOUNT)
//		 m_subGame->update(FrozenCommon::SGIDX_AMOUNT, FrozenCommon::BASE_AMOUNT);
}

void
ElToroSlotGuts::spin(SlotSpin *slotSpin)
{
	int32 bonus;
    SpinLine *spinLine;

    SlotGuts::spin(slotSpin);
	m_complete = true;
	bonus = m_subGame->getDataAt(ElToroCommon::SGIDX_BONUS);
//	bonus = 0;
	if (bonus == 0)
	{
	    spinLine = slotSpin->getSpinLineAt(0);
		if ( spinLine->payouttypeid == ElToroCommon::EVT_BULLFIGHT )
		{
			
			m_subGame->update(ElToroCommon::SGIDX_BONUS, 1);
			m_subGame->update(ElToroCommon::SGIDX_SERVPICK, 0);
			m_subGame->update(ElToroCommon::SGIDX_USERPICK, 0);
			m_subGame->update(ElToroCommon::SGIDX_WINCREDITS, spinLine->payoutamount);
			spinLine->payoutamount = 0;
			m_complete = false;
		}
		else
		{
			m_subGame->update(ElToroCommon::SGIDX_BONUS, 0);
			m_subGame->update(ElToroCommon::SGIDX_SERVPICK, 0);
			m_subGame->update(ElToroCommon::SGIDX_USERPICK, 0);
			m_subGame->update(ElToroCommon::SGIDX_WINCREDITS, 0);
		}
	}
	else
	{
 		ASSERT(0);
	}
	setGameComplete (m_complete);
}

void ElToroSlotGuts::playBonusGame(uint8 bonusGameType, int32 bonusDataCount, int32 *bonusData)
{

	int32 user, server, credits, bonus;

	DEBUGMSG(("ElToroSlotGuts::playBonusGame - gametype=%d, datacount=%d", (int)bonusGameType, bonusDataCount));
	for(int32 i=0;i<bonusDataCount;i++)
		DEBUGMSG(("  bonusData: %d", bonusData[i]));


	bonus = m_subGame->getDataAt(ElToroCommon::SGIDX_BONUS);
		if (bonusGameType == 1)	//
	if (bonus == 1)
	{
		if (bonusGameType == 1)	//
		{
			if (bonusDataCount == 1)
			{
				DEBUGMSG(("Paytable: %d", bonusData[0]));

				if (bonusData[0] >= 1 && bonusData[0] <= 2)
				{
					user = bonusData[0];
					server = getrand()%2 +1;
					credits = (user == server)?ElToroCommon::BULLFIGHT_BONUS*getInfo()->creditValue:0;
					credits += m_subGame->getDataAt(ElToroCommon::SGIDX_WINCREDITS);
					m_subGame->update(ElToroCommon::SGIDX_BONUS, 0);
					m_subGame->update(ElToroCommon::SGIDX_USERPICK, user);
					m_subGame->update(ElToroCommon::SGIDX_SERVPICK, server);
					m_subGame->update(ElToroCommon::SGIDX_WINCREDITS, credits);
				
					//Write to the database
					createBonusGame(getSlotSpin(getSlotSpinCount()-1)->getGameNum(),0,1,1,user,credits);
					m_complete = true;
				}
				else
				{
					ASSERT(0);
				}
			}
			else
			{
				//bad msg
				ASSERT(0);
			}
		}
		else
		{
			//unkown bonus game
			ASSERT(0);
		}

		completeGame();

//		m_subGame->update(ElToroCommon::SGIDX_BONUS, 0);
//		m_subGame->update(ElToroCommon::SGIDX_SERVPICK, 0);
//		m_subGame->update(ElToroCommon::SGIDX_USERPICK, 0);
//		m_subGame->update(ElToroCommon::SGIDX_WINCREDITS, 0);
	}
	else
	{
		ASSERT(0);
	}

}

uint8
ElToroSlotGuts::classifyStops(uint8* stops,uint8 bet, uint8 line)
{
	uint8 result = ElToroCommon::EVT_LOSER;

	int numSeven = 0;
	int numBull = 0;
	int numMatador = 0;
	int numRose = 0;
	int numSword = 0;
	int numCape = 0;
	int numHat = 0;

    //  Do a preliminary run-through
    for ( uint8 i = 0; i < getReelCount(); i++ )
    {
        // Set the icon flags;
        switch ( getIcon(i, stops[i])->iconid )
        {
			case ElToroCommon::HAT:
				++numHat;
				break;
			case ElToroCommon::CAPE:
				++numCape;
				break;
			case ElToroCommon::SWORD:
				++numSword;
				break;
			case ElToroCommon::ROSE:
				++numRose;
				break;
			case ElToroCommon::MATADOR:
				++numMatador;
				break;
			case ElToroCommon::BULL:
				++numBull;
				break;
			case ElToroCommon::SEVEN:
				++numSeven;
				break;
        }
    }

	if ((numSeven + numRose) == 3)
	{
		result = ElToroCommon::EVT_3SEVENS;
	}
	if (((numSword + numRose) == 3) && (result == ElToroCommon::EVT_LOSER))
	{
		result = ElToroCommon::EVT_3SWORDS;
	}
	if (((numCape + numRose) == 3) && (result == ElToroCommon::EVT_LOSER))
	{
		result = ElToroCommon::EVT_3CAPES;
	}
	if (((numHat + numRose) == 3) && (result == ElToroCommon::EVT_LOSER))
	{
		result = ElToroCommon::EVT_3HATS;
	}
	if ((result == ElToroCommon::EVT_LOSER) && (bet == 3))
	{

		//
		if (numBull + numMatador == 2 )
		{
			result = ElToroCommon::EVT_BULLFIGHT;
		}
	}
	return result;
}

bool8 ElToroSlotGuts::isGameComplete()
{
	return m_complete;
}
